package com.ruoyi.report_ys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_ys.domain.bo.ReportYsDzkyqdsyBaDetailBo;
import com.ruoyi.report_ys.domain.vo.ReportYsDzkyqdsyBaDetailVo;
import com.ruoyi.report_ys.domain.ReportYsDzkyqdsyBaDetail;
import com.ruoyi.report_ys.mapper.ReportYsDzkyqdsyBaDetailMapper;
import com.ruoyi.report_ys.service.IReportYsDzkyqdsyBaDetailService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 岩石单轴抗压强度试验检测记录详细Service业务层处理
 *
 * @author zyb
 * @date 2024-10-28
 */
@RequiredArgsConstructor
@Service
@Transactional
public class ReportYsDzkyqdsyBaDetailServiceImpl implements IReportYsDzkyqdsyBaDetailService {

    private final ReportYsDzkyqdsyBaDetailMapper baseMapper;

    /**
     * 查询岩石单轴抗压强度试验检测记录详细
     */
    @Override
    public ReportYsDzkyqdsyBaDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询岩石单轴抗压强度试验检测记录详细列表
     */
    @Override
    public TableDataInfo<ReportYsDzkyqdsyBaDetailVo> queryPageList(ReportYsDzkyqdsyBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportYsDzkyqdsyBaDetail> lqw = buildQueryWrapper(bo);
        Page<ReportYsDzkyqdsyBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询岩石单轴抗压强度试验检测记录详细列表
     */
    @Override
    public List<ReportYsDzkyqdsyBaDetailVo> queryList(ReportYsDzkyqdsyBaDetailBo bo) {
        LambdaQueryWrapper<ReportYsDzkyqdsyBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportYsDzkyqdsyBaDetail> buildQueryWrapper(ReportYsDzkyqdsyBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportYsDzkyqdsyBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, ReportYsDzkyqdsyBaDetail::getExcelId, bo.getExcelId());
        lqw.eq(bo.getShijbh() != null, ReportYsDzkyqdsyBaDetail::getShijbh, bo.getShijbh());
        lqw.eq(bo.getDingmbc1() != null, ReportYsDzkyqdsyBaDetail::getDingmbc1, bo.getDingmbc1());
        lqw.eq(bo.getDingmbc2() != null, ReportYsDzkyqdsyBaDetail::getDingmbc2, bo.getDingmbc2());
        lqw.eq(bo.getDingmbc3() != null, ReportYsDzkyqdsyBaDetail::getDingmbc3, bo.getDingmbc3());
        lqw.eq(bo.getDingmbc4() != null, ReportYsDzkyqdsyBaDetail::getDingmbc4, bo.getDingmbc4());
        lqw.eq(bo.getDimbc1() != null, ReportYsDzkyqdsyBaDetail::getDimbc1, bo.getDimbc1());
        lqw.eq(bo.getDimbc2() != null, ReportYsDzkyqdsyBaDetail::getDimbc2, bo.getDimbc2());
        lqw.eq(bo.getDimbc3() != null, ReportYsDzkyqdsyBaDetail::getDimbc3, bo.getDimbc3());
        lqw.eq(bo.getDimbc4() != null, ReportYsDzkyqdsyBaDetail::getDimbc4, bo.getDimbc4());
        lqw.eq(bo.getJiemj() != null, ReportYsDzkyqdsyBaDetail::getJiemj, bo.getJiemj());
        lqw.eq(bo.getShijphzh() != null, ReportYsDzkyqdsyBaDetail::getShijphzh, bo.getShijphzh());
        lqw.eq(bo.getKangyqd() != null, ReportYsDzkyqdsyBaDetail::getKangyqd, bo.getKangyqd());
        return lqw;
    }

    /**
     * 新增岩石单轴抗压强度试验检测记录详细
     */
    @Override
    public Boolean insertByBo(ReportYsDzkyqdsyBaDetailBo bo) {
        ReportYsDzkyqdsyBaDetail add = BeanUtil.toBean(bo, ReportYsDzkyqdsyBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改岩石单轴抗压强度试验检测记录详细
     */
    @Override
    public Boolean updateByBo(ReportYsDzkyqdsyBaDetailBo bo) {
        ReportYsDzkyqdsyBaDetail update = BeanUtil.toBean(bo, ReportYsDzkyqdsyBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportYsDzkyqdsyBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除岩石单轴抗压强度试验检测记录详细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
